﻿@{
    Layout = "~/Views/Samples/Generic/_Index.cshtml";
}
@section existing_js_startupload
{
    @if (ViewBag.Project.ViewEngine != ViewEngine.WebForms)
    {
        <text>
        function startUpload()
        {
            if (validateUpload())
                kw("slickUpload").start();
        }
        </text>
    } else if (ViewBag.Project.IsAspNetAjax)
    {
        <text>
        function startUpload()
        {
            var slickUpload = kw("@SampleHelper.ResolveId("slickUpload")");

            if (Page_ClientValidate() && slickUpload)
                slickUpload.start();
        }
        </text>
    }
}

@section new_js
{
        function startEdit()
        {
            if (window.kw && kw("@SampleHelper.ResolveId("slickUpload")"))
                kw("@SampleHelper.ResolveId("slickUpload")").clear();

            var panel = document.getElementById("@SampleHelper.ResolveId("uploadPanel")");

            @if (ViewBag.Project.ViewEngine == ViewEngine.WebForms)
            {
            <text>
            if (!panel)
                panel = document.getElementById("@SampleHelper.ResolveId("uploadResultPanel")");
            </text>
            }
            panel.style.display = "none";

            document.getElementById("settingsBox").style.backgroundColor = "#ffffe0";
            document.getElementById("settingsBox").style.padding = "1em";
            document.getElementById("settingsBox").style.border = "1px solid #ccc";
            document.getElementById("settingsTable").style.width = "99%";
            window.setTimeout(function () { document.getElementById("settingsTable").style.width = "100%"; }, 1);

            document.getElementById("editSettingsButton").style.display = "none";

            document.getElementById("@SampleHelper.ResolveId("saveSettingsButton")").style.display = "";

            var spans = document.getElementById("settingsTable").getElementsByTagName("span");

            for (var i = 0; i < spans.length; i++)
                spans[i].style.display = "none";

            var inputs = document.getElementById("settingsTable").getElementsByTagName("input");

            for (var i = 0; i < inputs.length; i++)
            {
                inputs[i].style.display = "inline";
                @if (ViewBag.Project.ViewEngine == ViewEngine.WebForms)
                {
                <text>
                if (inputs[i].parentNode.style.display == "none")
                    inputs[i].parentNode.style.display = "inline";
                </text>
                }
            }
        }

        @if (ViewBag.Project.ViewEngine == ViewEngine.WebForms)
        {
        <text>
        function Validate_SlickUploadRequiredFiles(source, args)
        {
            args.IsValid = (kw("@SampleHelper.ResolveId("slickUpload")").get_Files().length > 0);
        }

        function Validate_SlickUploadValidFiles(source, args)
        {
            var files = kw("@SampleHelper.ResolveId("slickUpload")").get_Files();

            args.IsValid = true;

            for (var i = 0; i < files.length; i++)
            {
                if (!files[i].get_IsValid())
                {
                    args.IsValid = false;

                    return;
                }
            }
        }
        </text>
        }
        else
        {
        <text>
        function saveEdit()
        {
            var uploadForm = document.getElementById("uploadForm");

            uploadForm.action = "@SampleHelper.RenderStatement("Url.Action(\"Index\")")";

            uploadForm.submit();
        }

        function validateUpload()
        {
            var isValid = true;

            var files = kw("slickUpload").get_Files();

            if (document.getElementById("RequireFileSelection").checked)
            {
                var isRequiredValid = (files.length > 0);

                document.getElementById("requiredFilesValidator").style.display = (isRequiredValid ? "none" : "block");

                if (!isRequiredValid)
                    isValid = false;
            }

            if (!document.getElementById("AllowInvalidUpload").checked)
            {
                var isFilesValid = true;

                for (var i = 0; i < files.length; i++)
                {
                    if (!files[i].get_IsValid())
                    {
                        isFilesValid = false;

                        break;
                    }
                }

                document.getElementById("summaryValidator").style.display = (isFilesValid ? "none" : "block");

                if (!isFilesValid)
                    isValid = false;
            }

            return isValid;
        }
        </text>
        }
}

@section beforeUploadPanel
{
        <div style="margin-bottom:1em;" id="settingsBox">
            <table id="settingsTable" class="settings">
                <tbody>
                    <tr>
                        <th colspan="3" style="font-weight:bold;border-bottom:solid 1px #ccc">General Settings</th>
                    </tr>
                    <tr>
                        <th style="width:12em">Max files:</th>
                        <td style="vertical-align:middle">                            
                            <span>@SampleHelper.RenderStatement(SampleHelper.VaryByViewEngine("maxFilesTextBox.Text", "Model.MaxFiles", "Model.MaxFiles").ToString())</span>
                            @SampleHelper.InputFor("MaxFiles", "TextBox", "display:none")
                        </td>
                        <td style="text-align:right"><em>Number of files</em></td>
                    </tr>
                    <tr>
                        <th style="width:12em">Max file size:</th>
                        <td style="vertical-align:middle">                            
                            <span>@SampleHelper.RenderStatement(SampleHelper.VaryByViewEngine("maxFileSizeTextBox.Text", "Model.MaxFileSize", "Model.MaxFileSize").ToString())</span>
                            @SampleHelper.InputFor("MaxFileSize", "TextBox", "display:none")
                        </td>
                        <td style="text-align:right"><em>Size, in KB</em></td>
                    </tr>
                    <tr>
                        <th>Confirm navigate message:</th>
                        <td>
                            <span>@SampleHelper.RenderStatement(SampleHelper.VaryByViewEngine("confirmNavigateMessageTextBox.Text", "Model.ConfirmNavigateMessage", "Model.ConfirmNavigateMessage").ToString())</span>
                            @SampleHelper.InputFor("ConfirmNavigateMessage", "TextBox", "display:none")
                        </td>
                        <td style="text-align:right"><em>Prompt when user navigates during upload</em></td>
                    </tr>
                    <tr>
                        <th colspan="3" style="font-weight:bold;border-bottom:solid 1px #ccc">Validation</th>
                    </tr>
                    <tr>
                        <th>Require file selection:</th>
                        <td colspan="2">
                            @if (ViewBag.Project.Language == CodeLanguage.Cs)
                            {
                            <span>@SampleHelper.RenderStatement(SampleHelper.VaryByViewEngine("requireFileSelectionCheckBox.Checked", "Model.RequireFileSelection", "Model.RequireFileSelection").ToString() + " ? \"Yes\" : \"No\"")</span>
                            }
                            else
                            {
                            <span>@SampleHelper.RenderStatement("IIf(" + SampleHelper.VaryByViewEngine("requireFileSelectionCheckBox.Checked", "Model.RequireFileSelection", "Model.RequireFileSelection").ToString() + ", \"Yes\", \"No\")")</span>                                
                            }
                            @SampleHelper.InputFor("RequireFileSelection", "CheckBox", "display:none")
                        </td>
                    </tr>
                    <tr>
                        <th>Allow invalid upload:</th>
                        <td>
                            @if (ViewBag.Project.Language == CodeLanguage.Cs)
                            {
                            <span>@SampleHelper.RenderStatement(SampleHelper.VaryByViewEngine("allowInvalidUploadCheckBox.Checked", "Model.AllowInvalidUpload", "Model.AllowInvalidUpload").ToString() + " ? \"Yes\" : \"No\"")</span>
                            }
                            else
                            {
                            <span>@SampleHelper.RenderStatement("IIf(" + SampleHelper.VaryByViewEngine("allowInvalidUploadCheckBox.Checked", "Model.AllowInvalidUpload", "Model.AllowInvalidUpload").ToString() + ", \"Yes\", \"No\")")</span>                                
                            }
                            @SampleHelper.InputFor("AllowInvalidUpload", "CheckBox", "display:none")
                        </td>
                        <td style="text-align:right"><em>Allow uploads, even with invalid files</em></td>
                    </tr>
                    <tr>
                        <th>Valid extensions:</th>
                        <td>                            
                            <span>@SampleHelper.RenderStatement(SampleHelper.VaryByViewEngine("validExtensionsTextBox.Text", "Model.ValidExtensions", "Model.ValidExtensions").ToString())</span>
                            @SampleHelper.InputFor("ValidExtensions", "TextBox", "display:none")
                        </td>
                        <td style="text-align:right"><em>Comma seperated list of valid extensions</em></td>
                    </tr>
                    <tr>
                        <th>Per file type message:</th>
                        <td>                            
                            <span>@SampleHelper.RenderStatement(SampleHelper.VaryByViewEngine("fileTypeMessageTextBox.Text", "Model.FileTypeMessage", "Model.FileTypeMessage").ToString())</span>
                            @SampleHelper.InputFor("FileTypeMessage", "TextBox", "display:none")
                        </td>
                        <td style="text-align:right"><em>Displayed next to files with invalid types</em></td>
                    </tr>
                    <tr>
                        <th>Per file size message:</th>
                        <td>                            
                            <span>@SampleHelper.RenderStatement(SampleHelper.VaryByViewEngine("fileSizeMessageTextBox.Text", "Model.FileSizeMessage", "Model.FileSizeMessage").ToString())</span>
                            @SampleHelper.InputFor("FileSizeMessage", "TextBox", "display:none")
                        </td>
                        <td style="text-align:right"><em>Displayed next to files with invalid sizes</em></td>
                    </tr>
                    <tr>
                        <th>Summary message:</th>
                        <td>                            
                            <span>@SampleHelper.RenderStatement(SampleHelper.VaryByViewEngine("summaryMessageTextBox.Text", "Model.SummaryMessage", "Model.SummaryMessage").ToString())</span>
                            @SampleHelper.InputFor("SummaryMessage", "TextBox", "display:none")
                        </td>
                        <td style="text-align:right"><em>Summary validation message for invalid files</em></td>
                    </tr>
                </tbody>
            </table>
            <p style="margin:.5em 0">
                <a id="editSettingsButton" href="javascript:;" onclick="startEdit()" class="button">
                    <span><b class="icon settings"></b> Edit Settings</span>
                </a>
                @SampleHelper.RenderButton("saveSettingsButton", "button", "CausesValidation=\"false\"", "onclick=\"saveEdit()\"", "display:none", @<span><b class="icon save"></b> Save Settings</span>, includeWebFormsClick: false)
                <a id="clearSettingsButton" href="javascript:;" onclick="window.location = window.location" class="button">
                    <span><b class="icon cancel"></b> Clear Settings</span>
                </a>
            </p>
            <div style="clear:both"></div>
        </div>
}

@section beforeSlickUpload
{
    @if (ViewBag.Project.ViewEngine == ViewEngine.Mvc || ViewBag.Project.ViewEngine == ViewEngine.Razor)
    {
        Dictionary<string, string> attributes = (Dictionary<string, string>)Context.Items["SlickUploadAttributes"];

        attributes["MaxFiles"] = "Model.MaxFiles";
        attributes["ValidExtensions"] = "Model.ValidExtensions";
        attributes["MaxFileSize"] = "Model.MaxFileSize";
        attributes["ConfirmNavigateDuringUploadMessage"] = "Model.ConfirmNavigateMessage";
        attributes["InvalidExtensionMessage"] = "Model.FileTypeMessage";
        attributes["InvalidFileSizeMessage"] = "Model.FileSizeMessage";
    }
}

@section afterSlickUpload
{
                            @if (ViewBag.Project.ViewEngine == ViewEngine.WebForms)
                            {
                            <asp:CustomValidator ID="requiredFilesValidator" runat="server" ClientValidationFunction="Validate_SlickUploadRequiredFiles" Text="Please select at least one file to upload." Display="Dynamic" />
                            <asp:CustomValidator ID="summaryValidator" runat="server" ClientValidationFunction="Validate_SlickUploadValidFiles" Display="Dynamic" />
                            }
                            else
                            {
                            <div id="requiredFilesValidator" style="display:none">Please select at least one file to upload.</div>
                            <div id="summaryValidator" style="display:none">@SampleHelper.RenderStatement("Model.SummaryMessage")</div>                                
                            }
}